/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-25 11:03:30
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.core.service.impl;

import javax.transaction.Transactional;

import com.joysuch.wwyt.bp.enums.DataStateTypes;
import com.joysuch.wwyt.core.bean.BaseTypeGroupBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PinYinUtils;
import com.joysuch.wwyt.core.entity.BaseTypeGroup;
import com.joysuch.wwyt.core.repository.BaseTypeGroupDao;
import com.joysuch.wwyt.core.repository.BaseTypeGroupItemDao;
import com.joysuch.wwyt.core.service.BaseTypeGroupService;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.util.PageableUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

@Service
@Transactional
public class BaseTypeGroupServiceImpl implements BaseTypeGroupService {

    @Autowired
    private BaseTypeGroupDao dao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;
    @Autowired
    private BaseTypeGroupItemDao baseTypeGroupItemDao;

    /**
     * 增加字典类型
     *
     * @param bean
     * @return
     */
    @Override
    public BaseTypeGroup addType(BaseTypeGroupBean bean) {
        BaseTypeGroup entity = new BaseTypeGroup();
        entity.setName(bean.getName());
        //编码
        String abbrev = PinYinUtils.getHanziInitials(bean.getName());
        List<BaseTypeGroup> gList = dao.getListByCode(abbrev);
        if (!CollectionUtils.isEmpty(gList)) {
            abbrev += gList.size();
        }
        if (StringUtils.isBlank(abbrev)) {
            entity.setCode(bean.getName());
        } else {
            entity.setCode(abbrev);
        }
        entity.setState(DataStateTypes.EFFECTIVE.getState());
        BaseTypeGroup save = dao.save(entity);
        this.checkGroupName(save);
        return save;
    }

    /**
     * 修改字典类型
     *
     * @param bean
     * @return
     */
    @Override
    public BaseTypeGroup updateType(BaseTypeGroupBean bean) {
        BaseTypeGroup group = dao.findById(bean.getId()).orElseThrow(() -> new IllegalArgumentException("字典类型不存在"));
        group.setName(bean.getName());
        BaseTypeGroup save = dao.save(group);
        this.checkGroupName(save);
        return save;
    }

    private void checkGroupName(BaseTypeGroup g) {
        int count = dao.countByName(g.getName());
        if (count > 1) {
            throw new IllegalArgumentException("字典类型名称已存在");
        }
    }

    /**
     * 删除字典类型
     *
     * @param id
     */
    @Override
    public void deleteType(Long id) {
        BaseTypeGroup group = dao.findById(id).orElseThrow(() -> new IllegalArgumentException("字典类型不存在"));
        group.setState(DataStateTypes.UN_EFFECTIVE.getState());
        baseTypeGroupItemDao.deleteAllByTypeGroupIdIn(group.getId());
        dao.save(group);
    }

    /**
     * 分页展示字典类型
     *
     * @param page
     * @return
     */
    @Override
    public ResultBean pageType(Pageable page) {
        List<BaseTypeGroup> gList = dao.findByState(DataStateTypes.EFFECTIVE.getState());
        if (!CollectionUtils.isEmpty(gList)) {
            List<BaseTypeGroupBean> beanList = new ArrayList<>();
            for (BaseTypeGroup group : gList) {
                BaseTypeGroupBean bean = new BaseTypeGroupBean();
                BeanUtils.copyProperties(group, bean);
                beanList.add(bean);
            }
            List<BaseTypeGroupBean> list = PageableUtil.getList(page.getPageNumber(), page.getPageSize(), beanList);
            return ResultBean.pageData(list, gList.size());
        } else {
            return ResultBean.success(gList);
        }

    }

    /**
     * 获取字典列表
     *
     * @return
     */
    @Override
    public List<BaseTypeGroupBean> getTypeList() {
        List<BaseTypeGroup> gList = dao.findByState(DataStateTypes.EFFECTIVE.getState());
        List<BaseTypeGroupBean> beanList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(gList)) {
            for (BaseTypeGroup group : gList) {
                BaseTypeGroupBean bean = new BaseTypeGroupBean();
                BeanUtils.copyProperties(group, bean);
                beanList.add(bean);
            }
        }
        return beanList;
    }

    @Override
    public void update(BaseTypeGroup entity) throws Exception {
        BaseTypeGroup content = dao.findById(entity.getId()).get();
        if (content != null) {
            // TODO set nessessary properties
            dao.save(content);
        }
    }

    @Override
    public Page<BaseTypeGroup> findByPage(Pageable page) {
        return dao.findAll(page);
    }

    @Override
    public void delete(Long id) {
        baseTypeGroupItemDao.deleteAllByTypeGroupIdIn(id);
        dao.deleteById(id);
    }

    @Override
    public BaseTypeGroup findById(Long id) {
        return dao.findById(id).get();
    }


}
